package com.example.xd.permissionlibrary

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.content.ContextCompat

/**
 * @auther XuDan on 2020/8/28.
 */
object PermissionUtil {
    fun hasPermission(context: Context, permissions: Array<String>): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (i in 0..permissions.size - 1) {
                if (ContextCompat.checkSelfPermission(
                        context,
                        permissions.get(i)
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    return false
                }
            }
            return true
        }
        return true

    }

    fun getPermissions(
        context: Context,
        permissions: Array<String>
    ) {
        if (!hasPermission(context, permissions)) {
            val intent = Intent(context, PermissionActivity::class.java)
            intent.putExtra("permission", permissions)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }

    }

    interface PermissionResult {
        fun getPermissionOK()
        fun getPermissionNO()

    }

}